
package pcs;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import pcs.gui.RestrictionWindow;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author ahmet
 */
public class PostureWarningSystem {

    RestrictionWindow restrictionWindow;
    public PostureWarningSystem(RestrictionWindow restrictionWindow) {
        this.restrictionWindow = restrictionWindow;
    }

    private boolean isWindows() {
        return (System.getProperty("os.name").toLowerCase().indexOf("windows")!=-1);
    }

    private boolean isLinux() {
        return (System.getProperty("os.name").toLowerCase().indexOf("linux")!=-1);
    }

    public void popup(String message) {
        //Here we construct a popup window
        //and show on the computer screen
        final JDialog popupWindow = new JDialog();
        popupWindow.setTitle("Posture Correction Warning");
        popupWindow.setSize(300, 100);
        popupWindow.getContentPane().setLayout(new BorderLayout());

        JLabel warningLabel = new JLabel(message, JLabel.CENTER);

        JPanel jp = new JPanel(new FlowLayout());
        JButton closeButton = new JButton("Close");
        closeButton.setMnemonic('C');
        closeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                popupWindow.dispose();
            }
        });
        jp.add(closeButton);

        popupWindow.getContentPane().add(warningLabel, BorderLayout.CENTER);
        popupWindow.getContentPane().add(jp, BorderLayout.SOUTH);

        popupWindow.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        popupWindow.setAlwaysOnTop(true);
        popupWindow.setLocationRelativeTo(null);
        popupWindow.setVisible(true);
    }

    public void sound() {
        try {
            AudioPlayer.player.start(new AudioStream(new FileInputStream("ding.wav")));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void restriction() {
        //fill the screen with a dialog
        restrictionWindow.activate("Please correct your posture!", false, 100);
    }

    public void systemBeep(){
        Toolkit.getDefaultToolkit().beep();
    }

    public void badPosture() {
        SystemSettings systemSettings = new SystemSettings();

        if (systemSettings.isWarnDeteriorationWithPopup()) {
            popup("Please correct your posture!");
        }

        if (systemSettings.isWarnDeteriorationWithSound()) {
            sound();
        }

        if (systemSettings.isWarnDeteriorationWithRestriction()) {
            restriction();
        }
    }

    public void goodPosture() {
        restrictionWindow.setVisible(false);
    }
}